package com.youlai.boot.gym.controller;

import com.youlai.boot.gym.service.StoresService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.gym.model.form.StoresForm;
import com.youlai.boot.gym.model.query.StoresQuery;
import com.youlai.boot.gym.model.vo.StoresVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 门店管理前端控制层
 *
 * @author hxc
 * @since 2025-05-31 22:52
 */
@Tag(name = "门店管理接口")
@RestController
@RequestMapping("/api/v1/stores")
@RequiredArgsConstructor
public class StoresController  {

    private final StoresService storesService;

    @Operation(summary = "门店管理分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('gym:stores:query')")
    public PageResult<StoresVO> getStoresPage(StoresQuery queryParams ) {
        IPage<StoresVO> result = storesService.getStoresPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增门店管理")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('gym:stores:add')")
    public Result<Void> saveStores(@RequestBody @Valid StoresForm formData ) {
        boolean result = storesService.saveStores(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取门店管理表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('gym:stores:edit')")
    public Result<StoresForm> getStoresForm(
        @Parameter(description = "门店管理ID") @PathVariable Long id
    ) {
        StoresForm formData = storesService.getStoresFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改门店管理")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('gym:stores:edit')")
    public Result<Void> updateStores(
            @Parameter(description = "门店管理ID") @PathVariable Long id,
            @RequestBody @Validated StoresForm formData
    ) {
        boolean result = storesService.updateStores(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除门店管理")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('gym:stores:delete')")
    public Result<Void> deleteStoress(
        @Parameter(description = "门店管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = storesService.deleteStoress(ids);
        return Result.judge(result);
    }
}
